package com.zzl.study.nacos.loadbalancer;

import com.zzl.study.nacos.properties.NacosMultiDiscoveryProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

/**
 * Nacos负载均衡客户端配置类
 *
 * @author: zhangzl
 * @date: 2025/7/16 19:42
 * @version: 1.0
 */
@ConditionalOnDiscoveryEnabled
public class NacosLoadBalancerClientConfiguration {

    /**
     * 响应式服务实例提供者初始化顺序
     */
    private static final int REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER = 183827465;

    /**
     * 向Spring容器中注入一个Nacos负载均衡器
     *
     * @param environment                   Spring环境
     * @param loadBalancerClientFactory     负载均衡器工厂
     * @param nacosMultiDiscoveryProperties Nacos多注册中心属性配置
     * @return Nacos负载均衡器
     */
    @Bean
    @ConditionalOnMissingBean
    public ReactorLoadBalancer<ServiceInstance> nacosLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory, NacosMultiDiscoveryProperties nacosMultiDiscoveryProperties) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new NacosLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name, nacosMultiDiscoveryProperties);
    }

    /**
     * 响应式负载均衡配置
     */
    @Configuration(proxyBeanMethods = false)
    @ConditionalOnReactiveDiscoveryEnabled
    @Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER)
    public static class ReactiveSupportConfiguration {

        /**
         * 向Spring容器中注入一个默认的响应式负载均衡客户端提供者
         *
         * @param context Spring容器
         * @return 默认的响应式负载均衡客户端提供者
         */
        @Bean
        @ConditionalOnBean(ReactiveDiscoveryClient.class)
        @ConditionalOnMissingBean
        @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
        public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
            return ServiceInstanceListSupplier.builder()
                    .withDiscoveryClient()
                    .build(context);
        }

        /**
         * 向Spring容器中注入一个区域首选的响应式负载均衡客户端提供者
         *
         * @param context Spring容器
         * @return 区域首选的响应式负载均衡客户端提供者
         */
        @Bean
        @ConditionalOnBean(ReactiveDiscoveryClient.class)
        @ConditionalOnMissingBean
        @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-preference")
        public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
            return ServiceInstanceListSupplier.builder()
                    .withDiscoveryClient()
                    .withZonePreference()
                    .build(context);
        }

    }

    /**
     * 阻塞式负载均衡配置
     */
    @Configuration(proxyBeanMethods = false)
    @ConditionalOnBlockingDiscoveryEnabled
    @Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER + 1)
    public static class BlockingSupportConfiguration {

        /**
         * 向Spring容器中注入一个默认的阻塞式负载均衡客户端提供者
         *
         * @param context Spring容器
         * @return 默认的阻塞式负载均衡客户端提供者
         */
        @Bean
        @ConditionalOnBean(DiscoveryClient.class)
        @ConditionalOnMissingBean
        @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
        public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
            return ServiceInstanceListSupplier.builder()
                    .withBlockingDiscoveryClient()
                    .build(context);
        }

        /**
         * 向Spring容器中注入一个区域首选的阻塞式负载均衡客户端提供者
         *
         * @param context Spring容器
         * @return 区域首选的阻塞式负载均衡客户端提供者
         */
        @Bean
        @ConditionalOnBean(DiscoveryClient.class)
        @ConditionalOnMissingBean
        @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-preference")
        public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
            return ServiceInstanceListSupplier.builder()
                    .withBlockingDiscoveryClient()
                    .withZonePreference()
                    .build(context);
        }

    }

}
